package com.example.richobject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import androidx.annotation.Nullable;

import static com.example.richobject.ByteUtil.byteToInt;
import static com.example.richobject.ByteUtil.dumpByte;
import static com.example.richobject.ByteUtil.intToByte;
import static com.example.richobject.HexUtil.hexToByteArray;

public class BaseActivity extends Activity {

    private SocketClient socketClient = new SocketClient();
    private int CODE_MSG = 0x1001;


    @SuppressLint("HandlerLeak")
    Handler handlerBox = new Handler() {
        public void handleMessage(Message msg) {
            Toast.makeText(BaseActivity.this,"收集到数据",Toast.LENGTH_SHORT).show();
            initData();
        }


    };

    public void initData() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * 物品柜相关
     */
    int[] gnum = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
            0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
            0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
            0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40};

    /**
     * 发送数据
     */
    private byte[] addr = new byte[1];
    private byte[] commandLen = new byte[1];
    private byte[] commandName = new byte[1];
    private byte[] askNum = new byte[1];
    private byte[] command = new byte[6];


    public void sendControl(final String hostIP, final String port, int num) {

        int data = num - 1;
        addr[0] = (byte) 0x63;
        commandLen[0] = (byte) 0x05;
        commandName[0] = (byte) 0xA1;
        askNum[0] = intToByte(gnum[data])[0];  //柜门编号
        LogUtil.d(">>>first==" + byteToInt(addr));
        LogUtil.d(">>>second==" + byteToInt(commandLen));
        LogUtil.d(">>>third==" + byteToInt(commandName));
        LogUtil.d(">>>forth==" + byteToInt(askNum));

        int all = byteToInt(addr) + byteToInt(commandLen) + byteToInt(commandName) + byteToInt(askNum);
        LogUtil.d(">>>总和==" + all);
        LogUtil.d(">>>总和转hexString==" + Integer.toHexString(all));
        LogUtil.d(">>>总和转byte==" + dumpByte(hexToByteArray(Integer.toHexString(all)), hexToByteArray(Integer.toHexString(all)).length));

        command[0] = addr[0];
        command[1] = commandLen[0];
        command[2] = commandName[0];
        command[3] = askNum[0];
        command[4] = hexToByteArray(Integer.toHexString(all))[0];
        command[5] = hexToByteArray(Integer.toHexString(all))[1];
        socketClient.sendData(command, hostIP, Integer.parseInt(port));

    }

    //开启子线程开始收集收据
    public void startSave(String hostIP,String port) {
        socketClient.startNetThread(handlerBox, hostIP, Integer.parseInt(port));
    }


    /**
     * 断开socket链接
     */
    public void closeSocket() {
        if (socketClient != null) {
            socketClient.closeSocket();
        }
    }

    //吐司提示
    public void showToast(String msg) {
        Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                socketClient.closeSocket();
            }
        }, 3000);
    }
}
